<?php

namespace app\models;

use think\Model;

/**
 * Class CourseContractModel
 * @package app\models
 */
class CourseContractModel extends Model
{
	protected $table = 'edu_course_contract_sign_up';

	/**
	 * @param $param
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return array
	 */
	public function getCourseContractLists($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = ['status' => array('in','1,2')];  //在1，2里面

		$status_infos = 0;
		if (isset($param['status_infos']) && '' !== $param['status_infos']) {
			$status_infos = $param['status_infos'];
		}

		if (isset($param['campus_name']) && '' !== $param['campus_name']) {
			$where['campus_name'] = ['like', $param['campus_name'].'%'];
		}
		if (isset($param['cid']) && '' !== $param['cid']) {
			$where['contract_id'] = $param['cid'];
		}else{
			return [];
		}

		if (isset($param['campus_id']) && '' !== $param['campus_id']) {
			$where['campus_id'] =  $param['campus_id'];
		}
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
		$listData = self::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$course = CourseModel::get(['id'=>$item['course_id']]);
				$item['price'] = $item['price'] / 100 ? $item['price'] / 100 : $course['price'] / 100;
				$item['course_name'] = $course['course_name'];
				$item['class_num'] = $item['class_num'] ? $item['class_num'] : 1;
				$item['total_price'] = $item['price'] * $item['class_num'];
				$item['discount_price'] = $item['price'] * $item['class_num'];
				$item['receivable_price'] = $item['price'] * $item['class_num'];
				if($status_infos == 1){
					$item['status_infos'] = 1;
				}else{
					$item['status_infos'] = 0;
				}
			}
		}
		return $listData;
	}


	/**
	 * 直接报名
	 * modify by Small Y
	 *
	 * @param $param
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function getDirectEnrolment($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		$where['student_id'] = $param['id'];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
		$listData = self::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$item['course_name'] = CourseModel::get($item['course_id'])['course_name'];
				$item['price'] = $item['price'] / 100;
			}
		}
		return $listData;
	}
}